﻿#pragma execution_character_set("utf-8")
#include "CGameEvent.h"
#include "../vector/Vector.h"
#include "../Particle/Particle.h"
#include "../PlayerData/PlayerData.h"
#include "../../dll_main.h"
#include "../messagebox/messagebox.h"

void IBeamTracerListener::Setup()
{	
	CGameEventManager::Get()->AddListener(&Get(), "round_start");
	CGameEventManager::Get()->AddListener(&Get(), "bullet_impact");
	CGameEventManager::Get()->AddListener(&Get(), "player_hurt");
	CGameEventManager::Get()->AddListener(&Get(), "player_death");
	CGameEventManager::Get()->AddListener(&Get(), "weapon_fire");
	CGameEventManager::Get()->AddListener(&Get(), "vote_cast");
	CGameEventManager::Get()->AddListener(&Get(), "item_purchase");
}

void IBeamTracerListener::FireGameEvent(CGameEvent* pEvent)
{
    if (!pEvent) return;

    std::string mzName = pEvent->GetName();
    auto& playerDataManager = PlayerDataManager::GetInstance();
    auto localPlayer = playerDataManager.GetLocalPlayerData();
    auto Players = playerDataManager.GetPlayersData();
    int localPlayerteam = localPlayer.team;
    if (strcmp(mzName.c_str(), "round_start") == 0)
    {
        Particle::m_pParticleManager = CGameParticleManager::Get();
        Particle::m_pParticleSystemMgr = IParticleSystemMgr::Get();
        return;
    }

    if (strcmp(mzName.c_str(), "bullet_impact") == 0 && all_settings::visual::ESP)
    {
        C_CSPlayerPawn* pShooter = pEvent->GetPlayerPawn("userid");
        if (!pShooter) return;

        C_BaseEntity* pEntity = static_cast<C_BaseEntity*>(pShooter);
        if (!pEntity) return;

        int team = pEntity->m_iTeamNum().value();
        auto eyePosOpt = pShooter->GetEyePos();
        if (!eyePosOpt.has_value()) return;

        Vector3 eyePos = eyePosOpt.value();
        Vector3 hitPos(
            pEvent->GetFloat("x"),
            pEvent->GetFloat("y"),
            pEvent->GetFloat("z")
        );

        if (pShooter == reinterpret_cast<C_CSPlayerPawn*>(localPlayer.pawn))
        {
            if (all_settings::visual::local::self::weapon_glow) {
                Particle::BulletTrace(eyePos, hitPos, all_settings::visual::local::self::weapon_glow_color);
            }
        }
        else
        {
            if (team == localPlayerteam && all_settings::visual::teammate::teammate_weapon_glow)
            {
                Particle::BulletTrace(eyePos, hitPos, all_settings::visual::teammate::teammate_weapon_glow_color);
            }
            if (team != localPlayerteam && all_settings::visual::enemy::enemy_weapon_glow)
            {
                Particle::BulletTrace(eyePos, hitPos, all_settings::visual::enemy::enemy_weapon_glow_color);
            }
        }
        return;
    }

    if (strcmp(mzName.c_str(), "player_hurt") == 0)
    {
        C_CSPlayerPawn* pVictim = pEvent->GetPlayerPawn("userid");
        if (!pVictim) return;

        C_CSPlayerPawn* pAttacker = pEvent->GetPlayerPawn("attacker");
        if (!pAttacker) return;

        if (pAttacker == reinterpret_cast<C_CSPlayerPawn*>(localPlayer.pawn))
        {
            int currentHealth = pVictim->m_iHealth().value();
            float damageTaken = pEvent->GetFloat("dmg_health");
            int remainingHealth = currentHealth;

            // 查找目标玩家
            PlayerData* pTargetPlayer = nullptr;
            for (auto& player : Players) {
                if (player.pawn == reinterpret_cast<uintptr_t>(pVictim)) {
                    pTargetPlayer = &player;
                    break;  // 找到后立即跳出循环
                }
            }
            if (!pTargetPlayer) return;

            // 处理击中音效
            if (all_settings::skin::voice::hit_sound)
            {
                Play_Sounds(all_settings::skin::voice::hit_sound_index);
            }

            // 处理击中消息
            if (all_settings::skin::voice::hit_message)
            {
                if (remainingHealth <= 0)
                {
                    MessageWindow::AddColoredMessage(
                        "[Sy Hex]我击中了 [player]%s[/player]，造成[damage]%.0f[/damage]伤害，对方已死亡",
                        pTargetPlayer->name.c_str(),
                        damageTaken
                    );
                }
                else
                {
                    MessageWindow::AddColoredMessage(
                        "[Sy Hex]我击中了 [player]%s[/player]，造成[success]%.0f[/success]伤害，对方剩余[health]%d[/health]血量",
                        pTargetPlayer->name.c_str(),
                        damageTaken,
                        remainingHealth
                    );
                }
            }
            if (all_settings::skin::voice::hit_death_message)
            {
                if (remainingHealth <= 0)
                {
                    std::string playername = all_settings::skin::voice::killerName;
                    std::string weaponName = all_settings::skin::voice::weaponName;

                    if (playername.empty())
                    {
                        playername = localPlayer.name;
                    }
                    if (weaponName.empty())
                    {
                        weaponName = localPlayer.weaponName;
                    }

                    std::string command = "say 你被" + playername + "用" + weaponName + "杀死";
                    engine_client->execute_client_cmd(command.c_str());
                }
            }
        }
        return;
    }
}
